package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.FinanceExample;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import java.io.InputStream;
import java.util.*;

@Controller
@Slf4j
@RequestMapping("/cargo/finance")
public class FinanceController extends BaseController {

    @Reference
    private FinanceService financeService;

    /**
     * 财务报运单列表显示
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        FinanceExample financeExample = new FinanceExample();
        PageInfo<Finance> pageInfo = financeService.findByPage(financeExample, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);

        return "cargo/finance/finance-list";
    }

    /**
     * 查看
     * http://localhost:8080/cargo/finance/toView.do?id=1
     */
    @RequestMapping("/toView")
    public String toView(String id) {
        // 根据id查询
        Finance finance = financeService.findById(id);
        request.setAttribute("finance",finance);
        return "cargo/finance/finance-view";
    }

    /**
     * 发票管理，点击财务报运单，进入财务报运单添加页面 参数接收发票实体类还是发票ID？
     * 到时要注入报运单，装箱，发票的业务类，查询好各种数据放到请求域跳到添加页面以显示
     */
    @RequestMapping("/tofinance")
    public String toExport(String id) {//接受多个id的格式{id1,id2,id3,}
        request.setAttribute("id", id);
        return "cargo/finance/finance-toFinance";
    }

    /**
     * 添加
     */
    @RequestMapping("/edit")
    public String edit(Finance finance) {
        // 设置企业信息（从登陆用户中获取）
        finance.setCompanyId(getLoginCompanyId());
        finance.setCompanyName(getLoginCompanyName());

        // 判断：根据id是否为空判断执行添加还是修改
            // 设置创建时间、创建人、创建人的部门
            finance.setCreateTime(new Date());
            finance.setUpdateTime(new Date());
            finance.setCreateBy(getLoginUser().getId());
            finance.setCreateDept(getLoginUser().getDeptId());
            // 添加
            financeService.insert(finance);
        return "redirect:/cargo/finance/list";
    }

    /**
     * PDF导出（8）导出报运详情
     */
    @RequestMapping("/financePdf")
    public void exportPdf8(String id) throws Exception {
        //1. 获取pdf模板文件流
        InputStream in = session.getServletContext().getResourceAsStream("/jasper/export_3.jasper");
        //A. 根据财务报运单id查询
        Finance finance = financeService.findById(id);
        // 对象转换为map
        Map<String, Object> map = BeanMapUtils.beanToMap(finance);
        //2. 创建JasperPrint对象，用于往模板中填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(in, map, new JREmptyDataSource());

        //3. 导出PDF
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-disposition","attachment;fileName=export.pdf");
        ServletOutputStream out = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,out);
        out.close();
    }


}
